package com.yami.shop.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.MeltCloudGroup;
import com.yami.shop.bean.model.MeltCloudUltraGroupApply;
import com.yami.shop.bean.model.MeltCloudUltraGroupCost;
import com.yami.shop.bean.model.MeltCloudUltraGroupRecord;
import com.yami.shop.bean.vo.UltraGroupApplyVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.service.MeltCloudUltraGroupApplyService;
import com.yami.shop.service.MeltCloudUltraGroupCostService;
import com.yami.shop.service.MeltCloudUltraGroupRecordService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.impl.RongYunGroupServiceImpl;
import com.yami.shop.user.common.service.UserGasInfoService;
import com.yami.shop.vo.group.GroupVo;
import com.yami.shop.vo.group.UserIdVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collections;

@RestController
@RequestMapping("/admin/ultra/group/apply")
@Tag(name = "融云超级群申请管理")
@AllArgsConstructor
public class MeltCloudUltraGroupApplyController {

    private final MeltCloudUltraGroupApplyService ultraGroupApplyService;
    
    private final MeltCloudUltraGroupRecordService ultraGroupRecordService;
    
    private final MeltCloudUltraGroupCostService ultraGroupCostService;
    
    private final RongYunGroupServiceImpl rongYunGroupService;
    
    private final ShopDetailService shopDetailService;
    
    private final UserGasInfoService userGasInfoService;

    @GetMapping("/page")
    @Operation(summary =  "申请超级群列表")
    public ServerResponseEntity<IPage<UltraGroupApplyVO>> page(PageParam<UltraGroupApplyVO> pageParam, @ParameterObject UltraGroupApplyVO applyVO){
        IPage<UltraGroupApplyVO> page = ultraGroupApplyService.page(pageParam, applyVO);
        return ServerResponseEntity.success(page);
    }

    
    @PostMapping("/audit")
    @Operation(summary =  "审核超级群申请")
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity audit(@RequestBody @Valid MeltCloudUltraGroupRecord record) {
        if (record.getStatus() == 1 && StringUtils.isEmpty(record.getReason())) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "驳回时请说明原因！");
        }
        @NotNull(message = "超级群聊申请id不能为空！") Long groupApplyId = record.getGroupApplyId();

        MeltCloudUltraGroupApply apply = ultraGroupApplyService.getById(groupApplyId);
        record.setName(apply.getName());
        record.setPic(apply.getPic());
        record.setIsOfficial(apply.getIsOfficial());
        ultraGroupRecordService.save(record);
        
        if (record.getStatus() == 1) {
            apply.setStatus(1);
            ultraGroupApplyService.updateById(apply);
        } else if (record.getStatus() == 0) {
            // 通过后扣减gas值并创建群
            rongYunGroupService.reduceGasAndCreateGroup(apply, true);
        } else {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "审核状态异常！");
        }
        return ServerResponseEntity.success();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    

}
